import { GranTypeEnum } from './../../enums/authEnum'
import { defHttp } from '/@/utils/http/axios'
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'

import { ErrorMessageMode } from '/#/axios'

enum Api {
  Login = '/oauth2/token',
  Logout = '/logout',
  GetUserInfo = '/sysUser/info',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
  Page = '/sysUser/page',
  Add = '/sysUser/add',
  UpdateBlackList = '/sysUser/updateBlackList',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  params.grant_type = GranTypeEnum.PASSWORD
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
      headers: {
        Authorization: 'Basic bWVzc2FnaW5nLWNsaWVudDpzZWNyZXQ=',
      },
      method: 'POST',
    },
    {
      errorMessageMode: mode,
      urlPrefix: '/auth',
    },
  )
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>(
    { url: Api.GetUserInfo },
    { errorMessageMode: 'none', urlPrefix: '/user' },
  )
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode })
}

export function doLogout() {
  return defHttp.get({ url: Api.Logout })
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  )
}

export function page(param) {
  return defHttp.get<any>({ url: Api.Page, params: param }, { urlPrefix: '/user' })
}

export function add(param) {
  return defHttp.post<null>(
    { url: Api.Add, params: param },
    { urlPrefix: '/user', errorMessageMode: 'modal', successMessage: 'message' },
  )
}

export function updateBlackList(userId) {
  return defHttp.post<null>(
    { url: Api.UpdateBlackList, params: { userId } },
    { urlPrefix: '/user', errorMessageMode: 'modal', successMessage: 'message' },
  )
}
